﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CADTool.DotNetARX
{
    public class CSHath
    {
        [CommandMethod("CSHathDemo")]
        public static void CSHathDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            //创建正六边形
            Polyline polygon = new Polyline();
            polygon.CreatePolygon(new Point2d(500, 200), 6, 30);
            //创建一个圆
            Circle circle = new Circle();
            circle.Center = new Point3d(500, 200, 0);
            circle.Radius = 10;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //将正六边形和圆添加到模型空间中
                ObjectId polygonId = db.AddToModelSpace(polygon);
                ObjectId circleId = db.AddToModelSpace(circle);
                //创建objectId集合类对象，用于存储填充边界ObjectId
                ObjectIdCollection ids = new ObjectIdCollection();
                ids.Add(polygonId);
                Hatch hatch = new Hatch();
                hatch.PatternScale = 0.5;
                string patterName = HatchTools.CurrentPattern;

                hatch.CreateHatch(HatchPatternType.PreDefined, patterName, true);

                hatch.AppendLoop(HatchLoopTypes.Outermost, ids);
                ids.Clear();
                ids.Add(circleId);

                hatch.AppendLoop(HatchLoopTypes.Default, ids);

                hatch.EvaluateHatch(true);

                trans.Commit();
            }
        }
    }
        

    public static class HatchTools
    {
        /// <summary>
        /// 渐变色填充图案名称
        /// </summary>
        public enum HatchGradientName
        {
            /// <summary>
            /// 线状渐变
            /// </summary>
            Linear,
            /// <summary>
            /// 圆柱状渐变
            /// </summary>
            Cylinder,
            /// <summary>
            /// 反圆柱状渐变
            /// </summary>
            Invcylinder,
            /// <summary>
            /// 球状渐变
            /// </summary>
            Spherical,
            /// <summary>
            /// 反球状渐变
            /// </summary>
            Invspherical,
            /// <summary>
            /// 半球状渐变
            /// </summary>
            Hemisperical,
            /// <summary>
            /// 反半球状渐变
            /// </summary>
            InvHemisperical,
            /// <summary>
            /// 抛物面状渐变
            /// </summary>
            Curved,
            /// <summary>
            /// 反抛物面状渐变
            /// </summary>
            Incurved
        }

        /// <summary>
        /// 设置或读取默认的填充图案
        /// </summary>
        public static string CurrentPattern
        {
            //获取HPNAME系统变量值
            get { return Application.GetSystemVariable("HPNAME").ToString(); }
            set
            {
                //如果要设置的值符合填充图案名，则设置HPNAME系统变量值
                if (value.Length <= 34 && !value.Contains(" ") && !string.IsNullOrEmpty(value) && value != CurrentPattern)
                {
                    Application.SetSystemVariable("HPNAME",value);
                }
            }
        }
        /// <summary>
        /// 创建图案填充
        /// </summary>
        /// <param name="hatch">填充对象</param>
        /// <param name="patternType">填充图案的类型</param>
        /// <param name="patternName">填充图案名称</param>
        /// <param name="associative">填充是否与边界关联</param>
        public static void CreateHatch(this Hatch hatch,HatchPatternType patternType,string patternName,bool associative)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            //设置填充属性为当前数据库默认值
            hatch.SetDatabaseDefaults();
            //设置图案填充的类型和填充图案名
            hatch.SetHatchPattern(patternType,patternName);
            //将填充添加到模型空间
            db.AddToModelSpace(hatch);
            //设置填充与边界是否关联
            hatch.Associative= associative? true:false;     
        }
        public static void CreateGradientHatch(this Hatch hatch,HatchGradientName gradientName,Color color1,Color color2,bool associative)
        {
            //设置渐变填充的类型所代表的字符串，用于将HatchGradientName枚举值转换为对应的字符串
            string[] gradientNames = new string[] { "Linear","Cylinder","Invcylinder","Spherical",
                                            "Invspherical","Hemisperical","InvHemisperical","Curved","Incurved"};
            Database db = HostApplicationServices.WorkingDatabase;
            //设置填充的默认值
            hatch.SetDatabaseDefaults();
            //设置填充的类型为渐变填充
            hatch.HatchObjectType=HatchObjectType.GradientObject; 
            //设置渐变填充的类型和图案名称
            hatch.SetGradient(GradientPatternType.PreDefinedGradient, gradientNames[(int)gradientName]);
            //新建两个Color类对象，分别表示渐变填充的起始和结束颜色
            GradientColor gColor1 = new GradientColor(color1,0);
            GradientColor gColor2=new GradientColor(color2,0);
            //设置渐变色的起始和结束颜色
            hatch.SetGradientColors(new GradientColor[] { gColor1,gColor2});
            db.AddToModelSpace(hatch); //将填充添加到模型空间
            //设置填充与边界是否关联
            hatch.Associative= associative? true:false; 
        }
    }
}
